package face;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.swing.table.DefaultTableModel;

import main.Programa;

public class DataModel extends DefaultTableModel {

	private ResultSet rs = null;
	private ResultSetMetaData meta = null;

	
	public DataModel(ResultSet rs) {

		if (rs != null)
		try {
			this.rs = rs;
			this.meta = rs.getMetaData();
			this.setColumnCount(meta.getColumnCount());
			
			
			montarModelo();
			
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			Programa.die(e);
		}
	}
	
	
	public void montarModelo() throws SQLException{
		
        String[] colsName = new String[getColumnCount()];
        
        
        	for (int i = 0 ; i < getColumnCount(); i++)
        	{
        		colsName[i] = (meta.getColumnName( i+1 ));
        	}
        	
        	this.setColumnIdentifiers(colsName);
        	
        	while (rs.next())
        	{
        		Object[] linha = new Object[getColumnCount()];
        		
        		for (int i = 0; i < getColumnCount(); i++) 
        		{
        			Object o = rs.getObject( i+1 );
        			
        			if(o != null)
        				linha[i] = o;
        			
        			else
        				linha[i] = "N/C";
        		}
        		addRow(linha);
        	}
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
